package domain;

import java.util.ArrayList;
import java.util.List;

public class Session {

    private Account currentAccount;
    private static Session session = null;

    private List<Withdrawl> withdrawls = new ArrayList<Withdrawl>(); //一次安全会话可进行多笔账户交易

    private Session() {
    }

    private Session(Account account) { //封装保证不被其他对象创建实例
        this.currentAccount = account;
    }

    public Account getAuthorizedAccount() {
        return currentAccount;
    }

    public static Session getSession(Account account) {
        if (session == null) {
            session = new Session(account);
        }
        return session;
    }


    public Withdrawl beginWithdrawlTransaction() {
        Withdrawl withdrawl = null;
        if (session != null) {
            withdrawl = new Withdrawl(session.getAuthorizedAccount());
            withdrawls.add(withdrawl);
        }
        return withdrawl;
    }

    //记录完成的取款操作
    public void recordWithdrawl(Withdrawl withdrawl, double amount) {
        try {

            int size = withdrawls.size() - 1;
            withdrawl = new Withdrawl(withdrawls.get(size), amount);
            withdrawls.remove(size);
            withdrawls.add(withdrawl);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    //返回取款对象
    public List<Withdrawl> getWithdrawls() {
        return withdrawls;
    }


}
